using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Gadgets.Accountings
{
	public partial class eCashAdvance : WeifenLuo.WinFormsUI.Docking.DockContent  
	{
		public eCashAdvance()
		{
			InitializeComponent();
		}
		
		private void eInputCashLoan_Load(object sender, EventArgs e)
		{
            try
            {
                txtTransDate.Value = DeclareLibrary.MARKET_DAY;
                ABSCommonLibrary.CreateGridView(ref gridControl1);
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
		}

        private bool CheckData()
        {

            if (txtAccountNo.Text == string.Empty)
            {
                ABSCommonLibrary.ShowMessage("Ban phai nhap so tai khoan cho vay ung truoc");
                txtAccountNo.Focus();
                return false;
            }

            if (Convert.ToDouble(txtLoanAmount.Value) <= 0)
            {
                ABSCommonLibrary.ShowMessage("So tien vay ung truoc phai lon hon 0");
                txtLoanAmount.Focus();
                return false;
            }
            if (Convert.ToDouble(txtLoanFee.Value) <= 0)
            {
                ABSCommonLibrary.ShowMessage("So tien phi vay ung truoc phai lon hon 0");
                txtLoanFee.Focus();
                return false;
            }
            return true;
        }

        private void AddnewCashAdvance()
        {
            string sQuestion = "so tien ban " + Convert.ToDouble(txtLoanAmount.Value) +
                " cua tai khoan " + txtAccountNo.Text;
            if (ABSCommonLibrary.ShowQuestion(
                "Ban muon thuc hien giao dich ung truoc " + sQuestion + " ?") == DialogResult.No)
                return;
            if (!CheckData())
                return;

            object[] p_param = new object[8];
            p_param[0] = DeclareLibrary.MARKET_DAY;
            p_param[1] = txtSettlementDay.Value;
            p_param[2] = 41;
            p_param[3] = txtLoanAmount.Value;
            p_param[4] = txtAccountNo.Text;
            p_param[5] = txtNotes.Text;            
            p_param[6] = DeclareLibrary.USER_NAME;
            p_param[7] = DeclareLibrary.MARKET_DAY;

            int error_code = Convert.ToInt32(ABSCommonLibrary.DoExecute(
                CommonLibrary.CommonLibrary.BOSC014003, ref p_param));
            switch (error_code)
            {
                case 0:
                    ABSCommonLibrary.ShowMessage("Giao dich ung truoc " + sQuestion + "  da duoc thuc hien");
                    LoadGrid();
                    break;
                case -1:
                    ABSCommonLibrary.ShowMessage("Khong ton tai tai khoan giao dich");                    
                    break;
                case -2:
                    ABSCommonLibrary.ShowMessage("Chua thiet lap tai khoan cho vay ung truoc");
                    break;
                case -3:
                    ABSCommonLibrary.ShowMessage("Khong dung ngay giao dich");
                    break;
                case -4:
                    ABSCommonLibrary.ShowMessage("So tien ung truoc vuot qua so cho phep");
                    break;
                default:
                    ABSCommonLibrary.ShowMessage("Giao dich ung truoc " + sQuestion + "  khong thuc hien duoc");
                    break;
            }
            
        }

        private void accountNo1_Validated(object sender, EventArgs e)
        {
            if (txtAccountNo.Value.Trim().Length != 10)
                return;
            if (txtTransDate.Checked == false)
                return;
            DataSet v_data = ABSCommonLibrary.DoInquiry(CommonLibrary.CommonLibrary.BOSC006002,
                new object[2] { txtAccountNo.Text, txtTransDate.Value });
            if (v_data != null)
            {
                DataRow row = v_data.Tables[0].Rows[0];
                txtSettlementDay.Value = Convert.ToDateTime(row[0]);
                txtTransAmount.Value = Convert.ToDecimal(row[1]);
                txtFeeTrade.Value = Convert.ToDecimal(row[2]);
                txtLoanDebited.Value = Convert.ToDecimal(row[3]);
                txtFeeRate.Value = Convert.ToDecimal(row[4]);
                txtDayLoan.Value =Math.Abs(Convert.ToDecimal(row[5]));
                txtCreditAmount.Value = Convert.ToDecimal(row[1]) -
                    Convert.ToDecimal(row[2]) - Convert.ToDecimal(row[3]);
            }
        }

        private void LoadGrid()
        {
            DataSet v_data = ABSCommonLibrary.DoInquiry(CommonLibrary.CommonLibrary.BOSC014008,
                new object[4] { DeclareLibrary.MARKET_DAY, txtAccountNo.Text, string.Empty, string.Empty });            
            gridControl1.SetDataBinding(v_data, v_data.Tables[0].TableName);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            try
            {
                AddnewCashAdvance();
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

        private void txtLoanAmount_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToDouble(txtLoanAmount.Value) > 0 &&
                Convert.ToDouble(txtFeeRate.Value) > 0)
                {
                    txtLoanFee.Value = Convert.ToDecimal(Convert.ToDouble(txtLoanAmount.Value) *
                    Convert.ToDouble(txtFeeRate.Value) * Convert.ToInt16(txtDayLoan.Value) / 100);

                    //txtCreditAmount.Value = Convert.ToDecimal(txtLoanAmount.Value) -
                    //    Convert.ToDecimal(txtLoanFee.Value);
                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

	}
}